package com.atguigu.gmall.fastdfs;

import org.junit.Test;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @Auther 赵乾坤
 * @Date 2020-12-26 12:47
 */
public class CompletableFutureDemo {

    @Test
    public void test01() throws ExecutionException, InterruptedException {
        //1创建线程(最简单的)
//        CompletableFuture<String> javaCompletableFuture =
//                CompletableFuture.completedFuture("JAVA");
//        System.out.println(javaCompletableFuture.get());
        //2创建打印线程名称的线程
//        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
//            System.out.println(Thread.currentThread().getName());
//        });
//        System.out.println(voidCompletableFuture.get());//打印线程名 没法返回值（默认自带的名）
//        CompletableFuture<String> uCompletableFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println(Thread.currentThread().getName());
//            return "supplyAsync";
//        });
//        System.out.println(uCompletableFuture.get());
    }
    //串行   任务1 -串行-》 任务2   任务2需要任务1的返回值
    @Test
    public void test2() throws Exception{
        //1:创建线程
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1开始");
            int i = 1 + 3;
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int j = 1/0;
            return i;
        });
        //2线程
        CompletableFuture<Void> voidCompletableFuture = completableFuture.thenAcceptAsync((t) -> {
            System.out.println("t是i吗：" + t);
        });


        //thenAccept() //表示：任务2直接使用任务1的线程
        //thenAccept Async () 表示： 任务2 去线程池获取一个新的线程来执行
        CompletableFuture<Integer> exceptionally = completableFuture.exceptionally((e) -> {//走异常打印异常
            System.out.println(e);
            return 1;
        });
        completableFuture.whenCompleteAsync((t,e)->{
            System.out.println(t);//返回值
            System.out.println(e);//异常
        });
        //等一会儿
//        completableFuture.join();
//        voidCompletableFuture.join();
        //等两个
        CompletableFuture.allOf(completableFuture, voidCompletableFuture).join();
        //表示2个等到一个就不等了
//        CompletableFuture.anyOf(completableFuture, voidCompletableFuture).join();

    }
    //并行
    @Test
    public void test3() throws Exception{
        //1:
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "AAAAA");
            return "hhh";
        });
        //2:
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {

            System.out.println(Thread.currentThread().getName()+ "BBBBBB");
        });
        System.out.println(Thread.currentThread().getName() + "TEST3");
        stringCompletableFuture.join();
        voidCompletableFuture.join();
    }
}
